<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use app\admin\model\ShippingAddress as Model;

class Shippingaddress extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if ($map == '') {
                $list = Db::name('shipping_address')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('shipping_address')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('shipping_address')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('shipping_address')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            // halt($list);

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function edit()
    {
        $data = $this->request->param();
        $info = Db::name('shipping_address')->where('id', $data['id'])->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function save()
    {
        $data = $this->request->param();
        $data['enterprise_id'] = $this->EnterpriseId;
        $model = new Model();
        if (isset($data['id'])) {
            $model->allowField(true)->save($data, ['id' => $data['id']]);
        } else {
            $model->allowField(true)->save($data);
        }

        if ($model) {
            return json(['status' => 1, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }

    public function del()
    {
        $data = $this->request->param();
        $res = Db::name('shipping_address')->where('id', $data['id'])->update(['is_del' => 1]);
        if ($res) {
            return json(['status' => 1, 'msg' => '删除成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除成功']);
        }
    }
}
